home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / SimpleWndCreate.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  10KB  |  274 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef _SIMPLEWNDCREATE_H
  30. #define _SIMPLEWNDCREATE_H
  31.  
  32. #include "../studio/services/svc_wndcreate.h"
  33. #include "../common/ptrlist.h"
  34. #include "../common/string.h"
  35. #include "../common/bucketitem.h"
  36.  
  37. // ===========================================================================
  38. //
  39. //    class ThingerBitmapInfo
  40. //
  41. //  This object simply contains all the info to describe the bitmaps used by
  42. // a component for its display in the Thinger.  This packages it nicely to be
  43. // used as a return value from functions.
  44. class ThingerBitmapInfo {
  45. public:
  46.   ThingerBitmapInfo(HINSTANCE inst, int normal, int pushed, int hilited, int activated) {
  47.     hInst       = inst;
  48.     _normal     = normal;
  49.     _pushed     = pushed;
  50.     _hilited    = hilited;
  51.     _activated  = activated;
  52.   }
  53. public:
  54.   HINSTANCE hInst;
  55.   int _normal;
  56.   int _pushed;
  57.   int _hilited;
  58.   int _activated;
  59. };
  60.  
  61. // ===========================================================================
  62. //
  63. //    template < class TWindowCreationObj >
  64. //    class SimpleWndCreateSvcBase
  65. //
  66. //  This template is used to streamline the window creation services, allowing
  67. // your component class or window class (or some other class, if you really
  68. // want) to control the creation and destruction of its window instances.
  69. //
  70. template < class TWindowCreationObj >
  71. class SimpleWndCreateSvcBase : public svc_windowCreateI {
  72. public:
  73.  
  74.   // ===========================================================================
  75.   //
  76.   // Because the instantiation of this template is to create
  77.   // a new service for each window in the system, we take the
  78.   // given "Window Name" and prepend a service tag to it.
  79.   static const char *getServiceName() { 
  80.     String serviceName = "WindowCreationService ";
  81.     serviceName += TWindowCreationObj::getWindowNameStatic();
  82.     return serviceName.getValue(); 
  83.   }
  84.  
  85.  
  86.   // ===========================================================================
  87.   //
  88.   // Wasabi is asking us if we can create this GUID's window
  89.   int testGuid(GUID g) {
  90.     // Test the GUID against our retrieved GUID.  If we match, say so.
  91.     if (g == TWindowCreationObj::getGUIDStatic()) {
  92.       return 1;
  93.     }
  94.     // If we do not, then we do not.
  95.     return 0;
  96.   }
  97.  
  98.  
  99.   // ===========================================================================
  100.   //
  101.   // The most basic class has no concept of window types or component buckets.
  102.   RootWnd *createWindowOfType(int windowtype, int n, RootWnd *parent) {
  103.     return NULL;
  104.   }
  105.  
  106.  
  107.   // ===========================================================================
  108.   //
  109.   // Create window according to GUID: if a window class does not support 
  110.   // multiple instances, you should keep a pointer to it and always return it 
  111.   // for its GUID, otherwise you should keep a list of your pointers for later 
  112.   // deletion.
  113.   RootWnd *createWindowByGuid(GUID g, RootWnd *parent) {
  114.     // Again test the given GUID against the WAC's GUID.
  115.     if (g == TWindowCreationObj::getGUIDStatic()) {
  116.       // If we match, create the window.
  117.       return TWindowCreationObj::createWindowStatic(0, parent);
  118.     }
  119.     // If that's not our guid, something is funky.  Return NULL.
  120.     return NULL; 
  121.   }
  122.  
  123.   // ===========================================================================
  124.   //
  125.   //  This callback method is called upon program exit, when the user destroys a window,
  126.   // or when the skin is switched.
  127.   //
  128.   // WIP: I'm still confused about the callchain for destroyWindow.  I am not very
  129.   // sure what window pointers I should expect to be called with or what I should
  130.   // do with those pointers.
  131.   int destroyWindow(RootWnd *w) {
  132.     return TWindowCreationObj::destroyWindowStatic(w);
  133.   }
  134. };
  135.  
  136.  
  137.  
  138. // ===========================================================================
  139. //
  140. //    template < class TWindowCreationObj >
  141. //    class ThingerWndCreateSvc
  142. //
  143. //  This template extends the functionality of the above template to include
  144. // an icon into the "Component Bucket" -- Colloquially named "The Thinger"
  145. // in Winamp 3 discourse.  (Actually "The Thinger" is just the name of the
  146. // component whose purpose is to display the components listed in the 
  147. // single Component Bucket in Winamp 3, whereas your program may have multiple
  148. // Component Buckets, etc etc).  So now you know what a "Bucket" is, right?
  149. //
  150. //  Won't that make life so much easier?
  151. template < class TWindowCreationObj >
  152. class ThingerWndCreateSvc : public SimpleWndCreateSvcBase< TWindowCreationObj > {
  153. public:
  154.  
  155.   // ===========================================================================
  156.   //
  157.   // Populate a component bucket with a specific type of window. This function is called
  158.   // as long as you do not return NULL, with n incremented for each pass.
  159.   //
  160.   // NOTE: The only window type you will be called with, for now, is a type of
  161.   // "WindowTypes::BUCKETITEM" -- and unless you want to use multiple window types,
  162.   // don't worry about returning anything but NULL for values of n greater than 0;
  163.   RootWnd *createWindowOfType(int windowtype, int n, RootWnd *parent) {
  164.     switch (windowtype) {
  165.       case WindowTypes::BUCKETITEM: {  
  166.         switch (n) {
  167.           case 0: {
  168.             // If you wanna play with the kids in the thinger, you'd better
  169.             // be prepared to fly your colors, proudly.
  170.             ThingerBitmapInfo bmp = TWindowCreationObj::getThingerBitmapInfoStatic();
  171.  
  172.             BucketItem *bucketitem = new BucketItem;
  173.             bucketitem->setAutoOpen(TWindowCreationObj::getGUIDStatic());
  174.             bucketitem->setAutoText(TWindowCreationObj::getWindowNameStatic());
  175.             bucketitem->setBitmaps(bmp.hInst, bmp._normal, bmp._pushed, bmp._hilited, bmp._activated);
  176.             bucketitem_list.addItem(bucketitem);
  177.             return bucketitem;
  178.           }
  179.         }
  180.       }
  181.     }
  182.     return NULL;
  183.   }
  184.  
  185.   // ===========================================================================
  186.   //
  187.   //  This callback method is called upon program exit, when the user destroys 
  188.   // a window, or when the skin is switched.
  189.   //
  190.   // WIP: SHOULD THIS CALL THE DESTROY WINDOW STATIC?
  191.   // FG> Not yet but it will have to, I'll change it when needed
  192.   int destroyWindow(RootWnd *w) {
  193.     // Doublecheck that our bucketitem_list has this window pointer.
  194.     if (bucketitem_list.haveItem(w)) { 
  195.       bucketitem_list.removeItem(w); 
  196.       delete static_cast<BucketItem *>(w); // This is safe, because we already checked it.
  197.     } else {
  198.       // If we don't have it, we can't delete it.  Tell the API.
  199.       return 0;
  200.     }
  201.     return 1;
  202.   }
  203.  
  204. private:
  205.   PtrList<RootWnd> bucketitem_list;
  206. };
  207.  
  208.  
  209. #if 0 // Example code
  210. //
  211. // This class shows what static methods must be provided in the class
  212. // used to handle the window creation.  How this info is actually used
  213. // for making WASABI componentry is discussed in Example1 and ExampleB.
  214. //
  215. // Please see how this code is ACTUALLY used in BOTH of those examples
  216. // before attempting to use this code on your own.  There are two standard
  217. // ways for providing the object functionality to come from the component
  218. // class or from the window class.
  219. //
  220. // ===========================================================================
  221. //
  222. class SampleWindowCreationObj {
  223. private:
  224.   static SampleWindowCreationObj *     myInstance;
  225. public:
  226.   SampleWindowCreationObj() {
  227.     // Only allow a single instance to be constructed.
  228.     ASSERT( myInstance == NULL );
  229.     myInstance = this;
  230.   }
  231.  
  232.   static SampleWindowInfoObj & Main() {
  233.     // Make that instance available as a static item.
  234.     return * myInstance;
  235.   }
  236.   //  
  237.   //  Therefore, if we wish, we can call down to that instance
  238.   // to handle any of the necessary function calls required by
  239.   // the template.  Hooray for legacy code support.
  240.   //
  241.  
  242.   //
  243.   //  The rest of the methods listed here are the required static
  244.   // methods for any object that is passed into the above template.
  245.   //
  246.   // =========================================================================
  247.   //
  248.   static const char *getWindowNameStatic() { 
  249.     return Main().getWindowName(); 
  250.   }
  251.   //
  252.   static GUID getGUIDStatic() { 
  253.     return Main().getGUID(); 
  254.   }
  255.   //
  256.   static RootWnd *createWindowStatic(int n, RootWnd *parentWnd) {
  257.     return Main().createWindow(n, parentWnd);
  258.   }
  259.   //
  260.   static int destroyWindowStatic(RootWnd *deadWnd) {
  261.     return Main().destroyWindow(deadWnd);
  262.   }
  263.   //
  264.   // If you are going to be using the Thinger template, 
  265.   // you also must provide the following method:
  266.   //
  267.   static ThingerBitmapInfo TWindowCreationObj::getThingerBitmapInfoStatic() {
  268.     return Main().getThingerBitmapInfo();
  269.   }
  270. }
  271. #endif// Example code
  272.  
  273. #endif//_SIMPLEWNDCREATE_H
  274.